import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UserGUI extends JFrame{
	
	private JPanel content;
	
	private JButton inputTimeZone;
	private JButton inputStartTime;
	private JButton inputEndTime;
	private JButton inputClassification;
	private JButton inputComment;
	private JButton inputLocation;
	private JButton inputResources;
	private JButton inputRecurring;
	//private JButton[] inputs;
	
	private JLabel outputTimeZone;
	private JLabel outputStartTime;
	private JLabel outputEndTime;
	private JLabel outputClassification;
	private JLabel outputComment;
	private JLabel outputLocation;
	private JLabel outputResources;
	private JLabel outputRecurring;
	//private JLabel[] outputs;
	
	private JButton reset;
	private JButton save;
	
	private Event myEvent;
	
	/*
	private static final String NAMES[] = {"Time Zone", "Start Time", "End Time", "Classification", "Comment", "Location", "Resources", "Recurring"};
	private static final int TIME_ZONE = 0;
	private static final int START_TIME = 1;
	private static final int END_TIME = 2;
	private static final int CLASSIFICATION = 3;
	private static final int COMMENT = 4;
	private static final int LOCATION = 5;
	private static final int RESOURCES = 6;
	private static final int RECURRING = 7;
	*/
	
	/**
	* GUI Constructor
	*/
	public UserGUI(){
	
		myEvent = new Event();
		
// initialize frame	
		this.content = new JPanel();
		content.setLayout(new BorderLayout());
		
// top		
		JLabel top = new JLabel("Create your ics file.");
		content.add(top, BorderLayout.PAGE_START);

// center
		JPanel center = new JPanel();
		center.setLayout(new GridLayout(8,3));
		
		inputTimeZone = new JButton("Time Zone");
		inputStartTime = new JButton("Start Time");
		inputEndTime = new JButton("End Time");
		inputClassification = new JButton("Classification");
		inputComment = new JButton("Comment");
		inputLocation = new JButton("Location");
		inputResources = new JButton("Resources");
		inputRecurring = new JButton("Recurring");
		
		inputTimeZone.addActionListener(new MyListener());
		inputStartTime.addActionListener(new MyListener());
		inputEndTime.addActionListener(new MyListener());
		inputClassification.addActionListener(new MyListener());
		inputComment.addActionListener(new MyListener());
		inputLocation.addActionListener(new MyListener());
		inputResources.addActionListener(new MyListener());
		inputRecurring.addActionListener(new MyListener());
		
		outputTimeZone = new JLabel("Required");
		outputStartTime = new JLabel("Required");
		outputEndTime = new JLabel("Required");
		outputClassification = new JLabel("Required");
		outputComment = new JLabel("Optional");
		outputLocation = new JLabel("Optional");
		outputResources = new JLabel("Optional");
		outputRecurring = new JLabel("Optional");
		
//set colors to help user distinguish between required and optional fields		
		/*
		outputTimeZone.setForeground(Color.red);
		outputStartTime.setForeground(Color.red);
		outputEndTime.setForeground(Color.red);
		outputClassification.setForeground(Color.red);
		outputComment.setForeground(Color.green);
		outputLocation.setForeground(Color.green);
		outputResources.setForeground(Color.green);
		outputRecurring.setForeground(Color.green);
		*/
		
		String parser = "      ..............................................................      ";
		
		center.add(inputTimeZone);
		center.add(new JLabel(parser));		
		center.add(outputTimeZone);

		center.add(inputStartTime);
		center.add(new JLabel(parser));		
		center.add(outputStartTime);
		
		center.add(inputEndTime);
		center.add(new JLabel(parser));		
		center.add(outputEndTime);
		
		center.add(inputClassification);
		center.add(new JLabel(parser));		
		center.add(outputClassification);
		
		center.add(inputComment);
		center.add(new JLabel(parser));		
		center.add(outputComment);		
		
		center.add(inputLocation);
		center.add(new JLabel(parser));		
		center.add(outputLocation);
		
		center.add(inputResources);
		center.add(new JLabel(parser));		
		center.add(outputResources);
		
		center.add(inputRecurring);
		center.add(new JLabel(parser));		
		center.add(outputRecurring);
		
		content.add(center, BorderLayout.CENTER);

// bottom
		JPanel bottom = new JPanel();
		bottom.setLayout(new FlowLayout());
		
		reset = new JButton("Reset");		
		save = new JButton("Save");
		
		reset.addActionListener(new MyListener());		
		save.addActionListener(new MyListener());
		
		bottom.add(reset);
		bottom.add(save);

		content.add(bottom, BorderLayout.PAGE_END);
		
// finalize frame		
        this.setContentPane(content);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Team Boyton - ICS 314 - Final Project");
	} //end of constructor    
    
    private void pressTimeZone(){
    //not coded yet
    }
    
    private void pressStartTime(){
    	
		JPanel dateTime = new JPanel();
		
		JTextField monthField = new JTextField(2);
		JTextField dateField = new JTextField(2);
		JTextField yearField = new JTextField(4);
		JTextField hourField = new JTextField(2);
		JTextField minField = new JTextField(2);
		
		dateTime.add(monthField);
		dateTime.add(new JLabel(" / "));
		dateTime.add(dateField);
		dateTime.add(new JLabel(" / "));
		dateTime.add(yearField);
		dateTime.add(new JLabel("   -   "));		
		dateTime.add(hourField);
		dateTime.add(new JLabel(" : "));
		dateTime.add(minField);
		
		boolean errorCheck = true;
		
		while(errorCheck){
			int reply = JOptionPane.showConfirmDialog(this, dateTime, "Please enter date and time", JOptionPane.OK_CANCEL_OPTION);
			
			if(reply == JOptionPane.CANCEL_OPTION){
				errorCheck = false;
			}
			else{
				try{
					int month = Integer.parseInt(monthField.getText()) - 1;
					int date = Integer.parseInt(dateField.getText());
					int year = Integer.parseInt(yearField.getText());
					int hour = Integer.parseInt(hourField.getText());
					int min = Integer.parseInt(minField.getText());

					myEvent.setStart(year,month,date,hour,min);
					errorCheck = false;
					outputStartTime.setText(myEvent.guiStart());
					
				} catch (IllegalArgumentException e){
					JOptionPane.showMessageDialog(this, e.toString() + "\nPlease re-enter a valid date & time.");
				}
			}
		} //end loop
    }
    
    private void pressEndTime(){
    	
		JPanel dateTime = new JPanel();
		
		JTextField monthField = new JTextField(2);
		JTextField dateField = new JTextField(2);
		JTextField yearField = new JTextField(4);
		JTextField hourField = new JTextField(2);
		JTextField minField = new JTextField(2);
		
		dateTime.add(monthField);
		dateTime.add(new JLabel(" / "));
		dateTime.add(dateField);
		dateTime.add(new JLabel(" / "));
		dateTime.add(yearField);
		dateTime.add(new JLabel("   -   "));		
		dateTime.add(hourField);
		dateTime.add(new JLabel(" : "));
		dateTime.add(minField);
		
		boolean errorCheck = true;
		
		while(errorCheck){
			int reply = JOptionPane.showConfirmDialog(this, dateTime, "Please enter date and time", JOptionPane.OK_CANCEL_OPTION);
			
			if(reply == JOptionPane.CANCEL_OPTION){
				errorCheck = false;
			}
			else{
				try{
					int month = Integer.parseInt(monthField.getText()) - 1;
					int date = Integer.parseInt(dateField.getText());
					int year = Integer.parseInt(yearField.getText());
					int hour = Integer.parseInt(hourField.getText());
					int min = Integer.parseInt(minField.getText());

					myEvent.setEnd(year,month,date,hour,min);
					errorCheck = false;
					outputEndTime.setText(myEvent.guiEnd());
					
				} catch (IllegalArgumentException e){
					JOptionPane.showMessageDialog(this, "\nPlease re-enter a valid date & time.");
				}
			}
		}
    }
    
    private void pressClassification(){
    	Object[] options = {"PUBLIC", "PRIVATE", "CONFIDENTIAL"};
    	String input = (String) JOptionPane.showInputDialog(this, "Please select a classification", "Selection Screen", 								JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
    	if(input == null){
    		return;
    	}
    	outputClassification.setText(input);
    	myEvent.setClassification(input);
    }
    
    private void pressComment(){
    	String input = JOptionPane.showInputDialog(this,"Please enter comment");
    	if(input == null || input.isEmpty()){
    		return;
    	}
    	outputComment.setText(input);
    	myEvent.setComment(input);
    }
    
    private void pressLocation(){
    	String input = JOptionPane.showInputDialog(this,"Please enter location");
    	if(input == null || input.isEmpty()){
    		return;
    	}
    	outputLocation.setText(input);
    	myEvent.setLocation(input);
    }
    
    private void pressResources(){
    	String input = JOptionPane.showInputDialog(this,"Please enter resources");
    	if(input == null || input.isEmpty()){
    		return;
    	}
    	outputResources.setText(input);
    	myEvent.setResources(input);
    }
    
    private void pressRecurring(){
    //not coded yet
    }
    
   	private void pressReset(){
		outputTimeZone.setText("Required");
		outputStartTime.setText("Required");
		outputEndTime.setText("Required");
		outputClassification.setText("Required");
		outputComment.setText("Optional");
		outputLocation.setText("Optional");
		outputResources.setText("Optional");
		outputRecurring.setText("Optional");	
   	}
    
    private void pressSave(){
    	//if any fields still contain "Required"
   		JOptionPane.showMessageDialog(this, "Please fill in all Required fields.", "alert", JOptionPane.ERROR_MESSAGE);
   		//else, call filewriter
    	System.out.println(myEvent);
    }
    
    class MyListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	        
        	JButton pressedButton = (JButton) e.getSource();

        	if(pressedButton == inputTimeZone){
        		pressTimeZone();
        	}
        	if(pressedButton == inputStartTime){
        		pressStartTime();
        	}
        	if(pressedButton == inputEndTime){
        		pressEndTime();
        	}
        	if(pressedButton == inputClassification){
        		pressClassification();
        	}
        	if(pressedButton == inputComment){
        		pressComment();
        	}
        	if(pressedButton == inputLocation){
        		pressLocation();
        	}
        	if(pressedButton == inputResources){
        		pressResources();
        	}
        	if(pressedButton == inputRecurring){
        		pressRecurring();
        	}        	
        	if(pressedButton == reset){
        		pressReset();
        	}
        	if(pressedButton == save){
        		pressSave();
        	}	
        }
    }//end inner class MyListener	
}